package plus.easydo.server.user.mp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.R;
import plus.easydo.core.vaild.Insert;
import plus.easydo.core.vaild.Update;
import plus.easydo.server.user.mp.dto.TenantDto;
import plus.easydo.server.user.mp.qo.TenantQo;
import plus.easydo.server.user.mp.service.ITenantService;
import plus.easydo.server.user.mp.vo.TenantVo;
import plus.easydo.starter.log.annotation.SystemLog;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.mybatis.plus.result.MPDataResult;
import plus.easydo.starter.oauth.resources.anotation.CustomizePreAuthorize;

import javax.annotation.Resource;
import java.util.List;

/**
 * 租户管理Controller
 * 
 * @author gebilaoyu
 * @date 2021-07-14 15:01:22
 */
@Api("租户管理")
@RestController
@RequestMapping("/tenant")
public class TenantController extends MPBaseController
{
    @Resource
    private ITenantService tenantService;

    /**
     * 分页条件查询租户管理列表
     */
    @ApiOperation(value = "分页条件查询租户管理列表")
    @CustomizePreAuthorize(hasPermission = {"tenant:tenant:list"})
    @PostMapping("/page")
    public MPDataResult page(@RequestBody TenantQo qo) {
        IPage<TenantVo> page = tenantService.page(qo);
        return ok(page);
    }

    /**
     * 获取所有租户管理列表
     */
    @ApiOperation(value = "获取所有租户管理列表")
    @CustomizePreAuthorize(hasPermission = {"tenant:tenant:list"})
    @GetMapping("/list")
    public R<List<TenantVo>> list(@RequestBody TenantQo qo) {
        return ok(tenantService.list(qo));
    }


    /**
     * 获取租户管理详细信息
     */
    @ApiOperation(value = "获取租户管理详细信息")
    @CustomizePreAuthorize(hasPermission = {"tenant:tenant:query"})
    @GetMapping(value = "/{id}")
    public R<TenantVo> getInfo(@PathVariable("id") String id){
        return ok(tenantService.selectById(id));
    }

    /**
     * 新增租户管理
     */
    @SystemLog(value = "新增租户",saveLog = true)
    @ApiOperation(value = "新增租户")
    @CustomizePreAuthorize(hasPermission = {"tenant:tenant:add"})
    @PostMapping
    public R<Object> add(@RequestBody @Validated(Insert.class) TenantDto dto){
        return opResult(tenantService.insert(dto));
    }

    /**
     * 修改租户管理
     */
    @SystemLog(value = "修改租户",saveLog = true)
    @ApiOperation(value = "修改租户")
    @CustomizePreAuthorize(hasPermission = {"tenant:tenant:edit"})
    @PutMapping
    public R<Object> edit(@RequestBody @Validated(Update.class) TenantDto dto)
    {
        return opResult(tenantService.update(dto));
    }

    /**
     * 删除租户管理
     */
    @SystemLog(value = "删除租户",saveLog = true)
    @ApiOperation(value = "删除租户")
    @CustomizePreAuthorize(hasPermission = {"tenant:tenant:remove"})
	@DeleteMapping("/{ids}")
    public R<Object> remove(@PathVariable String[] ids)
    {
        return opResult(tenantService.deleteByIds(ids));
    }
}
